home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / elfread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-01  |  12.2 KB  |  407 lines

  1. /* Read ELF (Executable and Linking Format) object files for GDB.
  2.    Copyright 1991, 1992 Free Software Foundation, Inc.
  3.    Written by Fred Fish at Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /************************************************************************
  22.  *                                    *
  23.  *                NOTICE                    *
  24.  *                                    *
  25.  * This file is still under construction.  When it is complete, this    *
  26.  * notice will be removed.  Until then, direct any questions or changes    *
  27.  * to Fred Fish at Cygnus Support (fnf@cygnus.com)            *
  28.  *                                    * 
  29.  * FIXME    Still needs support for shared libraries.        *
  30.  * FIXME    Still needs support for core files.            *
  31.  * FIXME    The ".debug" and ".line" section names are hardwired.    *
  32.  *                                    *
  33.  ************************************************************************/
  34.  
  35. #include "defs.h"
  36. #include "elf/common.h"
  37. #include "elf/external.h"
  38. #include "elf/internal.h"
  39. #include "bfd.h"
  40. #include "symtab.h"
  41. #include "symfile.h"
  42. #include "objfiles.h"
  43. #include "buildsym.h"
  44.  
  45. #define STREQ(a,b) (strcmp((a),(b))==0)
  46.  
  47. struct elfinfo {
  48.   unsigned int dboffset;    /* Offset to dwarf debug section */
  49.   unsigned int dbsize;        /* Size of dwarf debug section */
  50.   unsigned int lnoffset;    /* Offset to dwarf line number section */
  51.   unsigned int lnsize;        /* Size of dwarf line number section */
  52. };
  53.  
  54. static void
  55. elf_symfile_init PARAMS ((struct objfile *));
  56.  
  57. static void
  58. elf_new_init PARAMS ((struct objfile *));
  59.  
  60. static void
  61. elf_symfile_read PARAMS ((struct objfile *, CORE_ADDR, int));
  62.  
  63. static void
  64. elf_symfile_finish PARAMS ((struct objfile *));
  65.  
  66. static void
  67. elf_symtab_read PARAMS ((bfd *,  CORE_ADDR, struct objfile *));
  68.  
  69. static void
  70. record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
  71.                    struct objfile *));
  72.  
  73. static void
  74. elf_locate_sections PARAMS ((bfd *, asection *, PTR));
  75.  
  76. /* We are called once per section from elf_symfile_read.  We
  77.    need to examine each section we are passed, check to see
  78.    if it is something we are interested in processing, and
  79.    if so, stash away some access information for the section.
  80.  
  81.    For now we recognize the dwarf debug information sections and
  82.    line number sections from matching their section names.  The
  83.    ELF definition is no real help here since it has no direct
  84.    knowledge of DWARF (by design, so any debugging format can be
  85.    used).
  86.  
  87.    FIXME:  The section names should not be hardwired strings. */
  88.  
  89. static void
  90. elf_locate_sections (ignore_abfd, sectp, eip)
  91.      bfd *ignore_abfd;
  92.      asection *sectp;
  93.      PTR eip;
  94. {
  95.   register struct elfinfo *ei;
  96.  
  97.   ei = (struct elfinfo *) eip;
  98.   if (STREQ (sectp -> name, ".debug"))
  99.     {
  100.       ei -> dboffset = sectp -> filepos;
  101.       ei -> dbsize = bfd_get_section_size_before_reloc (sectp);
  102.     }
  103.   else if (STREQ (sectp -> name, ".line"))
  104.     {
  105.       ei -> lnoffset = sectp -> filepos;
  106.       ei -> lnsize = bfd_get_section_size_before_reloc (sectp);
  107.     }
  108. }
  109.  
  110. #if 0    /* Currently unused */
  111.  
  112. char *
  113. elf_interpreter (abfd)
  114.      bfd *abfd;
  115. {
  116.   sec_ptr interp_sec;
  117.   unsigned size;
  118.   char *interp = NULL;
  119.  
  120.   interp_sec = bfd_get_section_by_name (abfd, ".interp");
  121.   if (interp_sec)
  122.     {
  123.       size = bfd_section_size (abfd, interp_sec);
  124.       interp = alloca (size);
  125.       if (bfd_get_section_contents (abfd, interp_sec, interp, (file_ptr)0,
  126.                     size))
  127.     {
  128.       interp = savestring (interp, size - 1);
  129.     }
  130.       else
  131.     {
  132.       interp = NULL;
  133.     }
  134.     }
  135.   return (interp);
  136. }
  137.  
  138. #endif
  139.  
  140. /*
  141.  
  142. LOCAL FUNCTION
  143.  
  144.     record_minimal_symbol -- add entry to minimal symbol table
  145.  
  146. SYNOPSIS
  147.  
  148.     static void record_minimal_symbol (char *name, CORE_ADDR address)
  149.  
  150. DESCRIPTION
  151.  
  152.     Given a pointer to the name of a symbol that should be added to the
  153.     minimal symbol table and the address associated with that symbol, records
  154.     this information for later use in building the minimal symbol table.
  155.  
  156.  */
  157.  
  158. static void
  159. record_minimal_symbol (name, address, ms_type, objfile)
  160.      char *name;
  161.      CORE_ADDR address;
  162.      enum minimal_symbol_type ms_type;
  163.      struct objfile *objfile;
  164. {
  165.   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
  166.   prim_record_minimal_symbol (name, address, ms_type);
  167. }
  168.  
  169. /*
  170.  
  171. LOCAL FUNCTION
  172.  
  173.     elf_symtab_read -- read the symbol table of an ELF file
  174.  
  175. SYNOPSIS
  176.  
  177.     void elf_symtab_read (bfd *abfd, CORE_ADDR addr,
  178.                   struct objfile *objfile)
  179.  
  180. DESCRIPTION
  181.  
  182.     Given an open bfd, a base address to relocate symbols to, and a
  183.     flag that specifies whether or not this bfd is for an executable
  184.     or not (may be shared library for example), add all the global
  185.     function and data symbols to the minimal symbol table.
  186.  
  187. */
  188.  
  189. static void
  190. elf_symtab_read (abfd, addr, objfile)
  191.      bfd *abfd;
  192.      CORE_ADDR addr;
  193.      struct objfile *objfile;
  194. {
  195.   unsigned int storage_needed;
  196.   asymbol *sym;
  197.   asymbol **symbol_table;
  198.   unsigned int number_of_symbols;
  199.   unsigned int i;
  200.   struct cleanup *back_to;
  201.   CORE_ADDR symaddr;
  202.   enum minimal_symbol_type ms_type;
  203.   
  204.   storage_needed = get_symtab_upper_bound (abfd);
  205.  
  206.   if (storage_needed > 0)
  207.     {
  208.       symbol_table = (asymbol **) xmalloc (storage_needed);
  209.       back_to = make_cleanup (free, symbol_table);
  210.       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
  211.   
  212.       for (i = 0; i < number_of_symbols; i++)
  213.     {
  214.       sym = *symbol_table++;
  215.       /* Select global/weak symbols that are defined in a specific section.
  216.          Note that bfd now puts abs symbols in their own section, so
  217.          all symbols we are interested in will have a section. */
  218.       if ((sym -> flags & (BSF_GLOBAL | BSF_WEAK))
  219.           && (sym -> section != NULL))
  220.         {
  221.           symaddr = sym -> value;
  222.           /* Relocate all non-absolute symbols by base address.  */
  223.           if (sym -> section != &bfd_abs_section)
  224.         {
  225.           symaddr += addr;
  226.         }
  227.           /* For non-absolute symbols, use the type of the section
  228.          they are relative to, to intuit text/data.  Bfd provides
  229.          no way of figuring this out for absolute symbols. */
  230.           if (sym -> section -> flags & SEC_CODE)
  231.         {
  232.           ms_type = mst_text;
  233.         }
  234.           else if (sym -> section -> flags & SEC_DATA)
  235.         {
  236.           ms_type = mst_data;
  237.         }
  238.           else
  239.         {
  240.           ms_type = mst_unknown;
  241.         }
  242.           record_minimal_symbol ((char *) sym -> name, symaddr, ms_type, objfile);
  243.         }
  244.     }
  245.       do_cleanups (back_to);
  246.     }
  247. }
  248.  
  249. /* Scan and build partial symbols for a symbol file.
  250.    We have been initialized by a call to elf_symfile_init, which 
  251.    currently does nothing.
  252.  
  253.    ADDR is the address relative to which the symbols in it are (e.g.
  254.    the base address of the text segment).
  255.  
  256.    MAINLINE is true if we are reading the main symbol
  257.    table (as opposed to a shared lib or dynamically loaded file).
  258.  
  259.    This function only does the minimum work necessary for letting the
  260.    user "name" things symbolically; it does not read the entire symtab.
  261.    Instead, it reads the external and static symbols and puts them in partial
  262.    symbol tables.  When more extensive information is requested of a
  263.    file, the corresponding partial symbol table is mutated into a full
  264.    fledged symbol table by going back and reading the symbols
  265.    for real.  The function dwarf_psymtab_to_symtab() is the function that
  266.    does this for DWARF symbols.
  267.  
  268.    Note that ELF files have a "minimal" symbol table, which looks a lot
  269.    like a COFF symbol table, but has only the minimal information necessary
  270.    for linking.  We process this also, and just use the information to
  271.    add to gdb's minimal symbol table.  This gives us some minimal debugging
  272.    capability even for files compiled without -g.
  273.  */
  274.  
  275. static void
  276. elf_symfile_read (objfile, addr, mainline)
  277.      struct objfile *objfile;
  278.      CORE_ADDR addr;
  279.      int mainline;
  280. {
  281.   bfd *abfd = objfile->obfd;
  282.   struct elfinfo ei;
  283.   struct cleanup *back_to;
  284.   asection *text_sect;
  285.  
  286.   init_minimal_symbol_collection ();
  287.   back_to = make_cleanup (discard_minimal_symbols, 0);
  288.  
  289.   /* Compute the amount to relocate all symbols by.  The value passed in
  290.      as ADDR is typically either the actual address of the text section,
  291.      or a user specified address.  By subtracting off the actual address
  292.      of the text section, we can compute the relocation amount. */
  293.  
  294.   text_sect = bfd_get_section_by_name (objfile -> obfd, ".text");
  295.   addr -= bfd_section_vma (objfile -> obfd, text_sect);
  296.  
  297.   /* Process the normal ELF symbol table first. */
  298.  
  299.   elf_symtab_read (abfd, addr, objfile);
  300.  
  301.   /* Now process the DWARF debugging information, which is contained in
  302.      special ELF sections.  We first have to find them... */
  303.  
  304.   (void) memset ((char *) &ei, 0, sizeof (ei));
  305.   bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei);
  306.   if (ei.dboffset && ei.lnoffset)
  307.     {
  308.       dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)),
  309.                 bfd_get_filename (abfd),
  310.                 addr, mainline,
  311.                 ei.dboffset, ei.dbsize,
  312.                 ei.lnoffset, ei.lnsize, objfile);
  313.     }
  314.  
  315.   if (!have_partial_symbols ())
  316.     {
  317.       wrap_here ("");
  318.       printf_filtered ("(no debugging symbols found)...");
  319.       wrap_here ("");
  320.     }
  321.  
  322.   /* Install any minimal symbols that have been collected as the current
  323.      minimal symbols for this objfile. */
  324.  
  325.   install_minimal_symbols (objfile);
  326.  
  327.   do_cleanups (back_to);
  328. }
  329.  
  330. /* Initialize anything that needs initializing when a completely new symbol
  331.    file is specified (not just adding some symbols from another file, e.g. a
  332.    shared library).
  333.  
  334.    For now at least, we have nothing in particular to do, so this function is
  335.    just a stub. */
  336.  
  337. static void
  338. elf_new_init (ignore)
  339.      struct objfile *ignore;
  340. {
  341.   buildsym_new_init ();
  342. }
  343.  
  344. /* Perform any local cleanups required when we are done with a particular
  345.    objfile.  I.E, we are in the process of discarding all symbol information
  346.    for an objfile, freeing up all memory held for it, and unlinking the
  347.    objfile struct from the global list of known objfiles. */
  348.  
  349. static void
  350. elf_symfile_finish (objfile)
  351.      struct objfile *objfile;
  352. {
  353.   if (objfile -> sym_private != NULL)
  354.     {
  355.       mfree (objfile -> md, objfile -> sym_private);
  356.     }
  357. }
  358.  
  359. /* ELF specific initialization routine for reading symbols.
  360.  
  361.    It is passed a pointer to a struct sym_fns which contains, among other
  362.    things, the BFD for the file whose symbols are being read, and a slot for
  363.    a pointer to "private data" which we can fill with goodies.
  364.  
  365.    For now at least, we have nothing in particular to do, so this function is
  366.    just a stub. */
  367.  
  368. static void
  369. elf_symfile_init (ignore)
  370.      struct objfile *ignore;
  371. {
  372. }
  373.  
  374.  
  375. /*  Register that we are able to handle ELF object file formats and DWARF
  376.     debugging formats.
  377.  
  378.     Unlike other object file formats, where the debugging information format
  379.     is implied by the object file format, the ELF object file format and the
  380.     DWARF debugging information format are two distinct, and potentially
  381.     separate entities.  I.E. it is perfectly possible to have ELF objects
  382.     with debugging formats other than DWARF.  And it is conceivable that the
  383.     DWARF debugging format might be used with another object file format,
  384.     like COFF, by simply using COFF's custom section feature.
  385.  
  386.     GDB, and to a lesser extent BFD, should support the notion of separate
  387.     object file formats and debugging information formats.  For now, we just
  388.     use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
  389.     object file format and the DWARF debugging format. */
  390.  
  391. static struct sym_fns elf_sym_fns =
  392. {
  393.   "elf",        /* sym_name: name or name prefix of BFD target type */
  394.   3,            /* sym_namelen: number of significant sym_name chars */
  395.   elf_new_init,        /* sym_new_init: init anything gbl to entire symtab */
  396.   elf_symfile_init,    /* sym_init: read initial info, setup for sym_read() */
  397.   elf_symfile_read,    /* sym_read: read a symbol file into symtab */
  398.   elf_symfile_finish,    /* sym_finish: finished with file, cleanup */
  399.   NULL            /* next: pointer to next struct sym_fns */
  400. };
  401.  
  402. void
  403. _initialize_elfread ()
  404. {
  405.   add_symtab_fns (&elf_sym_fns);
  406. }
  407.